import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 * User: zm
 * Date: 2023/5/8
 */
public class Demo28 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int m = scan.nextInt();
        int[] stk = new int[100010];
        int tt = 0;
        while(m-- > 0){
            String s = scan.next();
            if(s.equals("push")){
                int x = scan.nextInt();
                stk[++tt] = x;
            }else if(s.equals("pop")){
                tt--;
            }else if(s.equals("empty")){
                if(tt > 0) System.out.println("NO");
                else System.out.println("YES");
            }else if(s.equals("query")){
                System.out.println(stk[tt]);
            }
        }
    }
}
